package com.analysis.service;

import com.analysis.entity.ProductDomain;
import com.analysis.mapper.ProductDomainMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class ProductDomainService {

    @Autowired
    private ProductDomainMapper productDomainMapper;

    public int createProductDomain(ProductDomain productDomain) {
        return productDomainMapper.insert(productDomain);
    }

    public int deleteProductDomain(Long id) {
        return productDomainMapper.deleteById(id);
    }

    public int updateProductDomain(ProductDomain productDomain) {
        return productDomainMapper.update(productDomain);
    }

    public ProductDomain getProductDomainById(Long id) {
        return productDomainMapper.selectById(id);
    }

    public List<ProductDomain> getAllProductDomains() {
        return productDomainMapper.selectAll();
    }

    public List<ProductDomain> listAll() {
        return productDomainMapper.listAll();
    }

    public ProductDomain getProductDomainByProductName(String productName) {
        return  productDomainMapper.selectByProductName(productName);
    }

    public List<ProductDomain> getProductDomainsByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return productDomainMapper.selectByPage(offset, pageSize);
    }

    public int countAllProductDomains() {
        return productDomainMapper.countAll();
    }

    /**
     * 根据产品名称列表获取域名映射
     * @param productNames 产品名称列表
     * @return 产品名称到域名的映射
     */
    public Map<String, String> getProductDomainMap(List<String> productNames) {
        Map<String, String> productDomainMap = new HashMap<>();
        if (productNames != null && !productNames.isEmpty()) {
            List<ProductDomain> productDomains = productDomainMapper.selectByProductNameList(productNames);
            for (ProductDomain productDomain : productDomains) {
                productDomainMap.put(productDomain.getProductName(), productDomain.getDomain());
            }
        }
        return productDomainMap;
    }

    /**
     * 获取所有产品域名映射
     * @return 产品名称到域名的映射
     */
    public Map<String, String> getAllProductDomainMap() {
        Map<String, String> productDomainMap = new HashMap<>();
        List<ProductDomain> productDomains = productDomainMapper.listAll();
        for (ProductDomain productDomain : productDomains) {
            if (productDomain.getEnableStatus() == 1) {
                productDomainMap.put(productDomain.getProductName(), productDomain.getDomain());
            }
        }
        return productDomainMap;
    }
}
